package com.dynabook.ems.disruptor.BorrowUpdate;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dynabook.ems.model.entity.EmsBorrowHistory;
import com.dynabook.ems.model.entity.EmsMyBorrow;
import com.dynabook.ems.model.enums.ModelStatusEnum;
import com.dynabook.ems.service.EmsBorrowHistoryService;
import com.dynabook.ems.service.EmsMyBorrowService;
import com.lmax.disruptor.WorkHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class BorrowUpdateEventHandler implements WorkHandler<BorrowUpdateEvent> {

    @Resource
    @Lazy
    private EmsBorrowHistoryService emsBorrowHistoryService;
    
    @Resource
    @Lazy
    private EmsMyBorrowService emsMyBorrowService;

    @Override
    public void onEvent(BorrowUpdateEvent event) throws Exception {
        List<EmsBorrowHistory> borrowHistoryList = event.getBorrowHistoryList();
        log.info("处理借用历史数据批次，数量: {}", borrowHistoryList.size());
        
        try {
           borrowHistoryList.parallelStream().forEach(item -> processItem(item));
            log.info("成功处理批次数据，数量: {}", borrowHistoryList.size());
        } catch (Exception e) {
            log.error("处理借用历史数据批次失败", e);
            throw e;
        }
    }
    
    private void processItem(EmsBorrowHistory item) {
        if(ObjectUtil.isNull(item.getEndDate()) && StrUtil.isBlank(item.getConfirmOperatorId()) && StrUtil.isBlank(item.getConfirmOperatorName())){
            UpdateWrapper<EmsBorrowHistory> emsBorrowHistoryUpdateWrapper = new UpdateWrapper<>();
            emsBorrowHistoryUpdateWrapper.eq("id", item.getId());
            emsBorrowHistoryUpdateWrapper.set("borrow_num", 1);
//            emsBorrowHistoryUpdateWrapper.set("return_num", null);
            emsBorrowHistoryUpdateWrapper.set("ems_request_id", "R" + item.getId());
            emsBorrowHistoryService.update(emsBorrowHistoryUpdateWrapper);
            
            EmsMyBorrow emsMyBorrow = new EmsMyBorrow();
            emsMyBorrow.setFixedNo(item.getFixedNo());
            emsMyBorrow.setEmsRequestId("R" + item.getId());
            emsMyBorrow.setBorrowNum(1);
            emsMyBorrow.setBorrowTime(item.getStartDate());
            emsMyBorrow.setUserName(item.getUserName());
            emsMyBorrow.setUserId(item.getUserId());
            emsMyBorrow.setRequestStatus(ModelStatusEnum.USING.getValue());
            emsMyBorrowService.save(emsMyBorrow);
        } else {
            UpdateWrapper<EmsBorrowHistory> emsBorrowHistoryUpdateWrapper = new UpdateWrapper<>();
            emsBorrowHistoryUpdateWrapper.eq("id", item.getId());
            emsBorrowHistoryUpdateWrapper.set("borrow_num", 1);
            emsBorrowHistoryUpdateWrapper.set("return_num", 1);
            emsBorrowHistoryUpdateWrapper.set("ems_request_id", "R" + item.getId());
            emsBorrowHistoryService.update(emsBorrowHistoryUpdateWrapper);
        }
    }
}